import uuid
import hashlib
import datetime
# from common.aes_encrypt import get_aes



class LicenseHelper(object):
    def generate_license(self, end_date, mac_addr):
        print("Received end_date: {}, mac_addr: {}".format(end_date, mac_addr))
        psw = self.hash_msg('smartant' + str(mac_addr))
        license_str = {}
        license_str['mac'] = mac_addr
        license_str['time_str'] = end_date
        license_str['psw'] = psw
        s = str(license_str)
        licence_result = get_aes().encrypt(s)
        return licence_result

    def get_mac_address(self):
        mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
        return ":".join([mac[e:e + 2] for e in range(0, 11, 2)])

    def hash_msg(self, msg):
        sha256 = hashlib.sha256()
        sha256.update(msg.encode('utf-8'))
        res = sha256.hexdigest()
        return res

    def read_license(self, license_result):
        lic_msg = bytes(license_result, encoding="utf8")
        license_str = get_aes().decrypt(lic_msg)
        license_dic = eval(license_str)
        return license_dic

    def check_license_date(self, lic_date):
        current_time = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S")
        current_time_array = datetime.datetime.strptime(current_time, "%Y-%m-%d %H:%M:%S")
        lic_date_array = datetime.datetime.strptime(lic_date, "%Y-%m-%d %H:%M:%S")
        remain_days = lic_date_array - current_time_array
        remain_days = remain_days.days
        if remain_days < 0 or remain_days == 0:
            return False
        else:
            return True

    def check_license_psw(self, psw):
        mac_addr = self.get_mac_address()
        hashed_msg = self.hash_msg('smartant' + str(mac_addr))
        if psw == hashed_msg:
            return True
        else:
            return False
    # def func(self):
    #     oper = LicenseHelper()
    #     read_bool, license_dic = oper.read_license(license)
    #     if not read_bool:
    #         res['status'] = False
    #         res['msg'] = "读取失败, 无效的License, 错误信息: {}".format(license_dic)
    #         return Response(res, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
    #     date_bool = oper.check_license_date(license_dic['time_str'])
    #     psw_bool = oper.check_license_psw(license_dic['psw'])
    #     if psw_bool:
    #         if date_bool:
    #             res['status'] = True
    #             res['time'] = license_dic['time_str']
    #             res['msg'] = ""
    #         else:
    #             res['status'] = False
    #             res['time'] = license_dic['time_str']
    #             res['msg'] = "激活码过期"
    #     else:
    #         res['status'] = False
    #         res['time'] = license_dic['time_str']
    #         res['msg'] = "MAC不匹配, License无效, 请更换License"
    #     if psw_bool and date_bool:
    #         serializer_content = {
    #             "license": license,
    #             "end_time": license_dic['time_str']
    #         }
    #         # license_serializer.save(**serializer_content)
    #         return Response(res, status=status.HTTP_200_OK)
    #     else:
    #         return Response(res, status=status.HTTP_422_UNPROCESSABLE_ENTITY)


t= LicenseHelper()
print(t.get_mac_address())
print(t.read_license())
